﻿using System;
using K9Nano.IM.Models;
using Microsoft.Extensions.Caching.Distributed;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;

namespace K9Nano.IM.Caching
{
    public class ChatAppCache
    {
        private readonly IDistributedCache _cache;
        private readonly IDataContext _db;

        public ChatAppCache(IDistributedCache cache, IDataContext db)
        {
            _cache = cache;
            _db = db;
        }

        public ChatApp Get(Guid id)
        {
            var key = "ChatApp:" + id;
            var result = _cache.Get<ChatApp>(key);
            if (result == null)
            {
                result = _db.Set<ChatApp>().Find(id);
                if (result != null)
                {
                    _cache.Add(key, result);
                }
            }
            return result;
        }
    }
}